package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.guesstv1.lib.model.TvPlayBean;
import com.stark.guesstv1.lib.model.TvPlayDataProvider;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityGame3Binding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.view.container.StkRelativeLayout;

public class Game3Activity extends BaseAc<ActivityGame3Binding> {

    //看图猜类型
    public static int level = 0;
    private List<TvPlayBean> listGame;
    private int rightNum;

    @Override
    protected int onCreate() {
        return R.layout.activity_game3;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivGame3Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDataBinding.rlGame3OptionA.setOnClickListener(this);
        mDataBinding.rlGame3OptionB.setOnClickListener(this);
        mDataBinding.rlGame3OptionC.setOnClickListener(this);
        mDataBinding.rlGame3OptionD.setOnClickListener(this);
        mDataBinding.tvGame3GoHome.setOnClickListener(this);
        mDataBinding.tvGame3Last.setOnClickListener(this);
        mDataBinding.ivGame3Tip.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.rlGame3OptionA://选项A
                isRight(1, mDataBinding.rlGame3OptionA, mDataBinding.tvGame3OptionA.getText().toString());
                break;
            case R.id.rlGame3OptionB://选项B
                isRight(2, mDataBinding.rlGame3OptionB, mDataBinding.tvGame3OptionB.getText().toString());
                break;
            case R.id.rlGame3OptionC://选项C
                isRight(3, mDataBinding.rlGame3OptionC, mDataBinding.tvGame3OptionC.getText().toString());
                break;
            case R.id.rlGame3OptionD://选项D
                isRight(4, mDataBinding.rlGame3OptionD, mDataBinding.tvGame3OptionD.getText().toString());
                break;
            case R.id.tvGame3Last://上一关
                if (level == 0) {
                    ToastUtils.showShort(R.string.it_is_already_the_first_level);
                    return;
                }
                level--;
                setData(listGame.get(level));
                break;
            case R.id.tvGame3GoHome://返回首页
                startActivity(HomeActivity.class);
                break;
            case R.id.ivGame3Tip://提示
                tip();
                break;
        }
    }

    @Override
    protected void initData() {
        listGame = TvPlayDataProvider.getDataForGuessTvType();
        if (listGame == null || listGame.size() == 0) return;
        setData(listGame.get(level));
    }

    //设置游戏数据
    private void setData(TvPlayBean bean) {
        mDataBinding.tvGame3Level.setText(getString(R.string.level_index_text, level + 1));
        Glide.with(this).load(bean.getPath()).into(mDataBinding.ivGame3Img);
        mDataBinding.tvGame3OptionA.setText("A." + bean.getOptiona());
        mDataBinding.tvGame3OptionB.setText("B." + bean.getOptionb());
        mDataBinding.tvGame3OptionC.setText("C." + bean.getOptionc());
        mDataBinding.tvGame3OptionD.setText("D." + bean.getOptiond());
        boolean isA = bean.checkAnswer(bean.getOptiona());
        boolean isB = bean.checkAnswer(bean.getOptionb());
        boolean isC = bean.checkAnswer(bean.getOptionc());
        boolean isD = bean.checkAnswer(bean.getOptiond());
        if (isA) {
            rightNum = 1;
        }
        if (isB) {
            rightNum = 2;
        }
        if (isC) {
            rightNum = 3;
        }
        if (isD) {
            rightNum = 4;
        }
    }

    //提示
    private void tip() {
        switch (rightNum) {
            case 1:
                isRight(rightNum, mDataBinding.rlGame3OptionA, mDataBinding.tvGame3OptionA.getText().toString());
                break;
            case 2:
                isRight(rightNum, mDataBinding.rlGame3OptionB, mDataBinding.tvGame3OptionB.getText().toString());
                break;
            case 3:
                isRight(rightNum, mDataBinding.rlGame3OptionC, mDataBinding.tvGame3OptionC.getText().toString());
                break;
            case 4:
                isRight(rightNum, mDataBinding.rlGame3OptionD, mDataBinding.tvGame3OptionD.getText().toString());
                break;
        }
    }

    //判断对错
    private void isRight(int kind, StkRelativeLayout viewBg, String rightAnswer) {
        if (kind == rightNum) {
            viewBg.setBackgroundResource(R.drawable.kp_xx2);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewBg.setBackgroundResource(R.drawable.kp_xx1);
                    //判断是否需要解锁下一关
                    if (level < (listGame.size() - 1)) {//如果当前关卡小于总关卡数
                        int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME3, 1);//获取记录通过关卡数
                        if (level == (spLevel - 1)) {//如果通过的是未解锁关卡则解锁下一关
                            SPUtil.putInt(mContext, Content.GAME_NAME3, spLevel + 1);
                        }
                    }
                    if (level == (listGame.size() - 1)) {
                        ToastUtils.showShort(R.string.it_is_already_the_last_level);
                        setData(listGame.get(level));
                        return;
                    }
                    level++;
                    setData(listGame.get(level));
                }
            }, 500);
        } else {
            viewBg.setBackgroundResource(R.drawable.kp_xx3);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewBg.setBackgroundResource(R.drawable.kp_xx1);
                }
            }, 500);
        }
    }

}